/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uet.questiondetector.utils;

import java.io.File;
import java.util.HashSet;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author thinhnk
 */
public class XmlTool {
    public static Set<String> readValuesByTag(String filename, String tagname) {
       Set<String> list = new HashSet<>();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db;
        try {
            //parse input
            db = dbf.newDocumentBuilder();
            Document doc = db.parse(new File(filename));
            NodeList nodelist = doc.getElementsByTagName(tagname);
            for (int i = 0; i < nodelist.getLength(); i++) {
                Node q = nodelist.item(i);
                list.add(q.getTextContent());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
